package com.wthink.hzgj

import android.app.Application
import android.content.Context
import com.wthink.hzgj.service.ScreenProvider
import com.wthink.hzgj.util.Log
import com.wthink.hzgj.util.SendToMainBoardNormalFrm
import com.wthink.hzgj.util.TcpHelp
import java.util.concurrent.TimeUnit

class BusApplication : Application() {
    companion object {
        lateinit var busContext: Context
    }

    override fun onCreate() {
        super.onCreate()
        busContext = applicationContext
        //拨号
        //Dial2box.start()
        //开机检查是否需要升级或继续升级
        ScreenProvider.checkUpgrade()
        Thread.setDefaultUncaughtExceptionHandler { t, e ->
            Log.error("异常崩溃", e)
            restart()
        }

/*        if (LeakCanary.isInAnalyzerProcess(this)) {
              return
          }
        LeakCanary.install(this)*/
        feedScreen()
    }

    fun feedScreen() {
        val tcpToken = TcpHelp.connect("localhost", 31001)
        val cmd = ByteArray(2)
        cmd[0] = 7
        cmd[1] = 120

        val food = SendToMainBoardNormalFrm(0x01, cmd, 2, 0x01)

        Thread {
            while (true) {
                TimeUnit.SECONDS.sleep(10)
                TcpHelp.send(tcpToken, food)
                TcpHelp.read(tcpToken)
            }
        }.start()
    }

    private fun restart() {
        try {
            val proc = Runtime.getRuntime()
                .exec(arrayOf("su", "-c", "reboot ")) //关机
            proc.waitFor()
        } catch (ex: Exception) {
            ex.printStackTrace()
        }
    }

}